//
//  NetRequiest.m
//  OutOfParents
//
//  Created by 汪的一声就哭了！ on 16/4/15.
//  Copyright © 2016年 汪的一声就哭了！. All rights reserved.
//

#import "NetRequiest.h"

@implementation NetRequiest
+ (void)handleDataWithString:(NSString *)string dictionary:(NSDictionary *)dictionary block:(void (^)(id responseObject))block
{
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *path;
    if (dictionary != nil) {
        path = [NSString stringWithFormat:@"%@/%lu%@.txt", docPath, (unsigned long)[string hash],[[dictionary allValues] firstObject]];
    }else{
        path = [NSString stringWithFormat:@"%@/%lu.txt", docPath, (unsigned long)[string hash]];
    }
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    if (dictionary != nil) {
        
        manager.responseSerializer = [AFCompoundResponseSerializer serializer];
        [manager POST:string parameters:dictionary progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if (responseObject != nil) {
                [NSKeyedArchiver archiveRootObject:responseObject toFile:path];
                block([NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil]);
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            id responseObject = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
            if (responseObject != nil) {
                block([NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil]);
            }
        }];
    }
    else{
        manager.responseSerializer = [AFCompoundResponseSerializer serializer];
        [manager GET:string parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if (responseObject != nil) {
            [NSKeyedArchiver archiveRootObject:responseObject toFile:path];
                block([NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil]);
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            id responseObject = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
            if (responseObject != nil) {
                block([NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil]);
            }
        }];
    }
}

//带进度的
+ (void)handleDataWithString:(NSString *)string dictionary:(NSDictionary *)dictionary HUD:(BOOL)hud  block:(void (^)(id responseObject))block
{
    if (hud) {
        [NetRequiest NetworkShowHUD];
    }
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *path;
    if (dictionary != nil) {
        path = [NSString stringWithFormat:@"%@/%lu%@.txt", docPath, (unsigned long)[string hash],[[dictionary allValues] firstObject]];
    }else{
        path = [NSString stringWithFormat:@"%@/%lu.txt", docPath, (unsigned long)[string hash]];
    }
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    if (dictionary != nil) {
        
        manager.responseSerializer = [AFCompoundResponseSerializer serializer];
        [manager POST:string parameters:dictionary progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if (responseObject != nil) {
                [NSKeyedArchiver archiveRootObject:responseObject toFile:path];
                block([NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil]);
                [NetRequiest NetworkHiddenHUD];
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"123455555:%@",error);
            id responseObject = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
            if (responseObject != nil) {
                block([NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil]);
                [NetRequiest NetworkHiddenHUD];
            }
        }];
    }
    else{
        manager.responseSerializer = [AFCompoundResponseSerializer serializer];
        [manager GET:string parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if (responseObject != nil) {
                [NSKeyedArchiver archiveRootObject:responseObject toFile:path];
                block([NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil]);
                [NetRequiest NetworkHiddenHUD];
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            id responseObject = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
            if (responseObject != nil) {
                block([NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil]);
                [NetRequiest NetworkHiddenHUD];
            }
        }];
    }
}
+(void)NetworkShowHUD{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [PTSProgressHUD show];
    });
    
}
+(void)NetworkHiddenHUD{
    
    [PTSProgressHUD hide];
}

@end
